<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_articles extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		return true;
		
	}
	public function create_article_type($data){
	$query="insert into `articles_type` (title,description,status,created_date,updated_date) VALUES ('".$data['title']."','".$data['description']."','1','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Article type successfully created');			
		}else{
			return array('message'=>'Article type Creation Failed');
		}	
	}
	public function update_article_type($data){
	$query="update articles_type set title='".$data['title']."',description='".$data['description']."',updated_date='".date("Y-m-d H:i:s")."' where article_type_id=".$data['article_type_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function update_article_type_status($data){

	$query="update articles_type set status='".$data['status']."',updated_date='".date("Y-m-d H:i:s")."' where article_type_id=".$data['article_type_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Status Updated Successfully');			
		}else{
			return array('message'=>'Update Failed');
		}	
	
	}
	public function get_article_type($data)
	{
		$query="select * from articles_type where  article_type_id='".$data['article_type_id']."'";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Article type data','result'=>$result[0]);			
		}else{
			return array('message'=>'No record');
		}	
	}
	public function get_all_types($data){
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query="select * from articles_type order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle('SELECT count(*) as total from articles_type');
		return array('message'=>"All Types","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Article Types');
		
	}
	public function create_article_author($data)
	{
		$query="insert into `articles_author` (title,first_name,last_name,middle_name,address1,address2,city,state,country,zip,phone1,phone2,email,description,created_by,created_date,updated_date) VALUES ('".$data['author_title']."','".$data['first_name']."','".$data['last_name']."','".$data['middle_name']."','".$data['address1']."','".$data['address2']."','".$data['city']."','".$data['state']."','".$data['country']."','".$data['zip']."','".$data['phone1']."','".$data['phone2']."','".$data['email']."','".$data['description']."','".$data['created_by']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Article author successfully created');			
		}else{
			return array('message'=>'Article author Creation Failed');
		}
	}
	public function update_article_author($data)
	{
		$query="update `articles_author` set title='".$data['author_title']."',first_name='".$data['first_name']."',last_name='".$data['last_name']."',middle_name='".$data['middle_name']."',address1='".$data['address1']."',address2='".$data['address2']."',city='".$data['city']."',state='".$data['state']."',country='".$data['country']."',zip='".$data['zip']."',phone1='".$data['phone1']."',phone2='".$data['phone2']."',email='".$data['email']."',description='".$data['description']."',updated_date='".date("Y-m-d H:i:s")."' where author_id='".$data['author_id']."'";
		//echo $query;exit;
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Article author successfully updated');			
		}else{
			return array('message'=>'Article author update Failed');
		}
	}
	public function delete_author($data){

	$query="update articles_author set status='".$data['status']."',updated_date='".date("Y-m-d H:i:s")."' where author_id=".$data['author_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Status Updated Successfully');			
		}else{
			return array('message'=>'Update Failed');
		}	
	
	}
	public function get_all_authors($data){
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		if($data['order_by']=="author_name")
		{
			$order_by="title";
		}else{
		$order_by=$data['order_by'];
		}
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	if(isset($data['search_by'])&& $data['search_by']!='')
	{
		if($data['search_by']=="status")
		{
			$query="select * from articles_author where status='".$data['search_by_value']."' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
			$query1="SELECT count(*) as total from articles_author where status='".$data['search_by_value']."'";
		}else if($data['search_by']=="email")
		{
		$query="select * from articles_author where email like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1="SELECT count(*) as total from articles_author where email like'%".$data['search_by_value']."%'";
		}
		else if($data['search_by']=="author_name"){
		$query="select * from articles_author where title like'%".$data['search_by_value']."%' or first_name like'%".$data['search_by_value']."%' or last_name like'%".$data['search_by_value']."%' or middle_name like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		
		$query1="SELECT count(*) as total from articles_author where title like'%".$data['search_by_value']."%' or first_name like'%".$data['search_by_value']."%' or last_name like'%".$data['search_by_value']."%' or middle_name like'%".$data['search_by_value']."%'";
		}
		else{
		$query="select * from articles_author order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1='SELECT count(*) as total from articles_author';
		}
	}else{
	$query="select * from articles_author order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1='SELECT count(*) as total from articles_author';
	}
	$result=$this->conn->dbMultiple($query);
		if($result){
		$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All Authors","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Authors');
		
	}
	public function create_article_disclaimer($data){
	$query="insert into `articles_disclaimer` (disclaimer_name,description,status,created_date,updated_date) VALUES ('".$data['disclaimer_name']."','".$data['description']."','1','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Disclaimer successfully created');			
		}else{
			return array('message'=>'Disclaimer Creation Failed');
		}	
	}
	public function update_disclaimer($data){
	$query="update articles_disclaimer set disclaimer_name='".$data['disclaimer_name']."',description='".$data['description']."',updated_date='".date("Y-m-d H:i:s")."' where disclaimer_id=".$data['disclaimer_id'];
	//echo $query;exit;
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Update Failed');
		}	
	
	}
	public function delete_disclaimer($data){

	$query="update articles_disclaimer set status='".$data['status']."',updated_date='".date("Y-m-d H:i:s")."' where disclaimer_id=".$data['disclaimer_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Status Updated Successfully');			
		}else{
			return array('message'=>'Update Failed');
		}	
	
	}
	public function get_disclaimers($data)
	{
		$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	if(isset($data['search_by'])&& $data['search_by']!='')
	{
		if($data['search_by']=="status")
		{
			$query="select * from articles_disclaimer where status='".$data['search_by_value']."' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
			$query1="SELECT count(*) as total from articles_disclaimer where status='".$data['search_by_value']."'";
		}else if($data['search_by']=="disclaimer_name")
		{
		$query="select * from articles_disclaimer where disclaimer_name like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1="SELECT count(*) as total from articles_disclaimer where disclaimer_name like'%".$data['search_by_value']."%'";
		}
		else if($data['search_by']=="description"){
		$query="select * from articles_disclaimer where description like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		
		$query1="SELECT count(*) as total from articles_disclaimer where description like'%".$data['search_by_value']."%'";
		}
		else{
		$query="select * from articles_disclaimer order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1='SELECT count(*) as total from articles_disclaimer';
		}
	}else{
	$query="select * from articles_disclaimer order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1='SELECT count(*) as total from articles_disclaimer';
	}
	$result=$this->conn->dbMultiple($query);
		if($result){
		$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All Disclaimers","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Disclaimers');
	}
	public function create_article($data)
	{
		$query="insert into `articles` (article_title,article_type_id,article_author_id,article_disclaimer_id,publish_date,expiry_date,description,status,created_date,updated_date) VALUES ('".$data['article_title']."','".$data['article_type_id']."','".$data['article_author_id']."','".$data['article_disclaimer_id']."','".$data['publish_date']."','".$data['expiry_date']."','".$data['description']."','1','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Article successfully created');			
		}else{
			return array('message'=>'Article Creation Failed');
		}
	}
	public function update_article($data)
	{
		$query="update `articles` set article_title='".$data['article_title']."',article_type_id='".$data['article_type_id']."',article_author_id='".$data['article_author_id']."',article_disclaimer_id='".$data['article_disclaimer_id']."',publish_date='".$data['publish_date']."',expiry_date='".$data['expiry_date']."',description='".$data['description']."',updated_date='".date("Y-m-d H:i:s")."' where article_id='".$data['article_id']."'";
		//echo $query;exit;
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Article successfully updated');			
		}else{
			return array('message'=>'Article update Failed');
		}
	}
	public function delete_article($data){
	$query="update articles set status='".$data['status']."',updated_date='".date("Y-m-d H:i:s")."' where article_id=".$data['article_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Status Updated Successfully');			
		}else{
			return array('message'=>'Update Failed');
		}	
	}
	public function get_articles($data)
	{
	$current_page=0;
	$results_per_page=10;
	$order_by="a.created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		if($data['order_by']=="author_name")
		{
			$order_by="au.title";
		}
		else{
		$order_by=$data['order_by'];
		}
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select a.article_id,a.article_title,a.publish_date,a.status,au.title author_title,au.first_name,au.middle_name,au.last_name,au.author_id,at.title as article_type,at.article_type_id
					from articles a
			        left join articles_author au on au.author_id=a.article_author_id
					left join articles_type at on at.article_type_id=a.article_type_id";
	$query_string1="select count(*) as total
					from articles a
			        left join articles_author au on au.author_id=a.article_author_id
					left join articles_type at on at.article_type_id=a.article_type_id";
	if(isset($data['search_by'])&& $data['search_by']!='')
	{
		if($data['search_by']=="status")
		{
			$query=$query_string." where a.status='".$data['search_by_value']."' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
			$query1=$query_string1." where a.status='".$data['search_by_value']."'";
		}else if($data['search_by']=="article_type")
		{
		$query=$query_string." where at.title like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1=$query_string1." where at.title like'%".$data['search_by_value']."%'";
		}
		else if($data['search_by']=="article_title"){
		$query=$query_string." where article_title like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		
		$query1=$query_string1." where article_title like'%".$data['search_by_value']."%'";
		}
		else if($data['search_by']=="author_name"){
		$query=$query_string." where au.title like'%".$data['search_by_value']."%' or au.first_name like'%".$data['search_by_value']."%' or au.middle_name like'%".$data['search_by_value']."%' or au.last_name like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		
		$query1=$query_string1." where au.title like'%".$data['search_by_value']."%' or au.first_name like'%".$data['search_by_value']."%' or au.middle_name like'%".$data['search_by_value']."%' or au.last_name like'%".$data['search_by_value']."%'";
		}else if($data['search_by']=="publish_date"){
		$query=$query_string." where publish_date like'%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		
		$query1=$query_string1." where publish_date like'%".$data['search_by_value']."%'";
		}
		else{
		$query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1=$query_string1;
		}
		}else{
		$query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1=$query_string1;
		}
		$result=$this->conn->dbMultiple($query);
		if($result){
		$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All Articles","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page,'order_by'=>$data['order_by'],'sort_order'=>$data['sort_order'],'search_by'=>$data['search_by'],'search_by_value'=>$data['search_by_value']);
		}
		else
		return array('message'=>'No Articles','current_page'=>$current_page,'results_per_page'=>$results_per_page,'order_by'=>$data['order_by'],'sort_order'=>$data['sort_order'],'search_by'=>$data['search_by'],'search_by_value'=>$data['search_by_value']);
	}
	public function get_article($data)
	{
		$query="select * from articles where  article_id='".$data['article_id']."'";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Article data','result'=>$result[0]);			
		}else{
			return array('message'=>'No records');
		}	
	}
	public function get_disclaimer($data)
	{
		$query="select * from articles_disclaimer where  disclaimer_id='".$data['disclaimer_id']."'";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Disclaimer data','result'=>$result[0]);			
		}else{
			return array('message'=>'No records');
		}	
	}
	public function get_author($data)
	{
		$query="select * from articles_author where  author_id='".$data['author_id']."'";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Author data','result'=>$result[0]);			
		}else{
			return array('message'=>'No records');
		}	
	}
	public function get_disclaimers_list($data)
	{
		$query="select disclaimer_id,disclaimer_name from articles_disclaimer where  status='1' order by disclaimer_name";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Disclaimers list','result'=>$result);			
		}else{
			return array('message'=>'No records');
		}	
	}
	public function get_authors_list($data)
	{
		$query="select author_id,title,first_name,middle_name,last_name from articles_author where  status='1' order by title";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Authors list','result'=>$result);			
		}else{
			return array('message'=>'No records');
		}	
	}
	public function get_articletype_list($data)
	{
		$query="select article_type_id,title from articles_type where  status='1' order by title";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Article types list','result'=>$result);			
		}else{
			return array('message'=>'No records');
		}	
	}
}
?>